package com.api.viewModel;

import android.app.Application;

import com.activity.bean.AttanceRecordBean;
import com.activity.bean.CategoryBean;
import com.activity.bean.ClassBean;
import com.activity.bean.ClassSpan;
import com.activity.bean.DateBean;
import com.activity.bean.EmployerBean;
import com.activity.bean.TagBean;
import com.activity.bean.UserBean;
import com.api.LoginApi;
import com.api.StoreApi;
import com.api.param.CategoryPagePram;
import com.api.param.CategoryParam;
import com.api.param.ClassParam;
import com.api.param.ClockSetParam;
import com.api.param.CommentParam;
import com.api.param.DeleteClassParam;
import com.api.param.GroupIdParam;
import com.api.param.GroupParam;
import com.api.param.PageParam;
import com.api.param.RankParam;
import com.api.param.RecordPageParam;
import com.api.param.SignParam;
import com.api.param.SortParam;
import com.api.param.StoreParam;
import com.api.param.UpdateCategoryParam;
import com.api.param.UserParam;
import com.api.response.ArrayListResponse;
import com.api.response.BaseResponsePredicate;
import com.api.response.OpusResponse;
import com.api.response.TodayResponse;
import com.example.http.BuildFactory;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class StoreVM extends BaseResponseViewModel {

    public StoreVM(@NonNull Application application) {
        super(application);
    }

    public final MutableLiveData<ArrayList<ClassBean>> attanceGroup = new MutableLiveData<>();
    public final MutableLiveData<ArrayList<ClassBean>> attanceGroupInfo = new MutableLiveData<>();
    public final MutableLiveData<ArrayList<EmployerBean>> members = new MutableLiveData<>();

    public final MutableLiveData<ArrayList<ClassSpan>> classSpan = new MutableLiveData<>();
    public final MutableLiveData<Boolean> addClass = new MutableLiveData<>();
    public final MutableLiveData<Boolean> deleteClass = new MutableLiveData<>();
    public final MutableLiveData<Boolean> updateClass = new MutableLiveData<>();
    public final MutableLiveData<String> addGroup = new MutableLiveData<>();
    public final MutableLiveData<String> updateGroup = new MutableLiveData<>();
    public final MutableLiveData<UserBean> clockSet = new MutableLiveData<>();
    public final MutableLiveData<ClassBean> clockSetRange = new MutableLiveData<>();
    public final MutableLiveData<String> deleteGroup = new MutableLiveData<>();

    public final MutableLiveData<ArrayList<AttanceRecordBean>> record = new MutableLiveData<>();
    public final MutableLiveData<ArrayList<TagBean>> tags = new MutableLiveData<>();
    public final MutableLiveData<ArrayList<CategoryBean>> categorys = new MutableLiveData<>();
    public final MutableLiveData<ArrayList<CategoryBean>> subCategorys = new MutableLiveData<>();
    public final MutableLiveData<ArrayListResponse<AttanceRecordBean>> recordPage = new MutableLiveData<>();
    public final MutableLiveData<ArrayListResponse<DateBean>> datePage = new MutableLiveData<>();
    public final MutableLiveData<Boolean> datePageError = new MutableLiveData<>();
    public final MutableLiveData<ArrayListResponse<UserBean>> checkMember = new MutableLiveData<>();
    public final MutableLiveData<Boolean>checkMemberError = new MutableLiveData<>();
    public final MutableLiveData<ArrayListResponse<AttanceRecordBean>> datRecord = new MutableLiveData<>();
    public final MutableLiveData<Boolean> datRecordError = new MutableLiveData<>();
    public final MutableLiveData<Boolean> recordPageError = new MutableLiveData<>();
    public final MutableLiveData<Boolean> updateCategory = new MutableLiveData<>();
    public final MutableLiveData<Boolean> addCategory = new MutableLiveData<>();
    public final MutableLiveData<Boolean> attanceSign = new MutableLiveData<>();
    public final MutableLiveData<Boolean> outgoingsign = new MutableLiveData<>();
    public final MutableLiveData<Boolean> otsign = new MutableLiveData<>();

    public final MutableLiveData<Boolean> sortCategory = new MutableLiveData<>();
    public final MutableLiveData<Boolean> deleteCategory = new MutableLiveData<>();
    public final MutableLiveData<Boolean> sign = new MutableLiveData<>();
    public final MutableLiveData<Boolean> sign2 = new MutableLiveData<>();
    public final MutableLiveData<TodayResponse> todayCheck = new MutableLiveData<>();
    public  Disposable getGroups (StoreParam param){


        return BuildFactory.getService(StoreApi.class).getGroups(json(param)).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {

                    attanceGroup.setValue(res.getResult());

                },throwable -> {
                    attanceGroup.setValue(null);
                    onError(throwable);

                });
    }

    public  Disposable getMember (StoreParam param){


        return BuildFactory.getService(StoreApi.class).getMmember(json(param)).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {

                    members.setValue(res.getResult());

                },throwable -> {
                    members.setValue(null);
                    onError(throwable);

                });
    }

    public  Disposable getClass (StoreParam param){


        return BuildFactory.getService(StoreApi.class).getClass(json(param)).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {

                    classSpan.setValue(res.getResult());

                },throwable -> {
                    classSpan.setValue(null);
                    onError(throwable);

                });
    }


    public  Disposable addClass (ClassParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).addClass(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {
                    addClass.setValue(true);
                    progress.setValue(false);
                    onError(new Throwable(res.getMessage()));

                },throwable -> {

                    onError(throwable);

                });
    }
    public  Disposable updateClass (ClassParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).updateClass(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {
                    updateClass.setValue(true);
                    progress.setValue(false);
                    onError(new Throwable(res.getMessage()));

                },throwable -> {

                    onError(throwable);

                });
    }

    public  Disposable deleteClass (DeleteClassParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).deleteClass(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {
                    deleteClass.setValue(true);
                    progress.setValue(false);

                    onError(new Throwable(res.getMessage()));

                },throwable -> {

                    onError(throwable);

                });
    }
    public  Disposable addGroup (GroupParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).addGroup(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {
                    addGroup.setValue(res.getResult());
                    progress.setValue(false);

                    onError(new Throwable(res.getMessage()));

                },throwable -> {

                    onError(throwable);

                });
    }

    public  Disposable updateGroup (GroupParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).updateGroup(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {
                    updateGroup.setValue(res.getResult());
                    progress.setValue(false);

                    onError(new Throwable(res.getMessage()));

                },throwable -> {

                    onError(throwable);

                });
    }
    public  Disposable deleteGroup (GroupIdParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).deleteGroup(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {
                    deleteGroup.setValue(res.getResult());
                    progress.setValue(false);

                    onError(new Throwable(res.getMessage()));

                },throwable -> {

                    onError(throwable);

                });
    }

    public  Disposable getGroupInfo (GroupParam param){


        progress.setValue(true);
        return BuildFactory.getService(StoreApi.class).getGroupinfo(json(param)).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {
                    progress.setValue(false);
                    attanceGroupInfo.setValue(res.getResult());

                },throwable -> {

                    onError(throwable);

                });
    }

    public  Disposable getMonthRecord (UserParam param){



        return BuildFactory.getService(StoreApi.class).getMonthRecord(json(param)).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {

                    record.setValue(res.getResult());

                },throwable -> {

                    record.setValue(null);
                    onError(throwable);

                });
    }

    public  Disposable getMonthPageRecord (RecordPageParam param,boolean isheader){



        return BuildFactory.getService(StoreApi.class).getMonthPageRecord(json(param)).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {

                    res.isHeader = isheader;
                    recordPage.setValue(res);

                },throwable -> {

                    recordPageError.setValue(isheader);
                    onError(throwable);

                });
    }

    public  Disposable getMemberPageRecord (RecordPageParam param,boolean isheader,int type){



        if(type==1){
            return BuildFactory.getService(StoreApi.class).getMemberRecordOut(json(param)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new BaseResponsePredicate())
                    .subscribe(res -> {

                        res.isHeader = isheader;
                        recordPage.setValue(res);

                    },throwable -> {

                        recordPageError.setValue(isheader);
                        onError(throwable);

                    });
        } if(type==2){
            return BuildFactory.getService(StoreApi.class).getusercheckinbyuseranddatespan_ot(json(param)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new BaseResponsePredicate())
                    .subscribe(res -> {

                        res.isHeader = isheader;
                        recordPage.setValue(res);

                    },throwable -> {

                        recordPageError.setValue(isheader);
                        onError(throwable);

                    });
        }
        else {
            return BuildFactory.getService(StoreApi.class).getMemberRecord(json(param)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new BaseResponsePredicate())
                    .subscribe(res -> {

                        res.isHeader = isheader;
                        recordPage.setValue(res);

                    },throwable -> {

                        recordPageError.setValue(isheader);
                        onError(throwable);

                    });
        }

    }


    public  Disposable getDateRecord (UserParam param,boolean isheader,int type){


        if(type==1){
            return BuildFactory.getService(StoreApi.class).getDateRecordOut(json(param)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new BaseResponsePredicate())
                    .subscribe(res -> {

                        res.isHeader = isheader;
                        datRecord.setValue(res);

                    },throwable -> {

                        datRecordError.setValue(isheader);
                        onError(throwable);

                    });
        } if(type==2){
            return BuildFactory.getService(StoreApi.class).getcheckinuserlistbydate_ot(json(param)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new BaseResponsePredicate())
                    .subscribe(res -> {

                        res.isHeader = isheader;
                        datRecord.setValue(res);

                    },throwable -> {

                        datRecordError.setValue(isheader);
                        onError(throwable);

                    });
        }else {
            return BuildFactory.getService(StoreApi.class).getDateRecord(json(param)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new BaseResponsePredicate())
                    .subscribe(res -> {

                        res.isHeader = isheader;
                        datRecord.setValue(res);

                    },throwable -> {

                        datRecordError.setValue(isheader);
                        onError(throwable);

                    });
        }

    }



    public  Disposable getDate (RecordPageParam param, boolean isheader,int type){


        if(type==1){
            return BuildFactory.getService(StoreApi.class).getDateOut(json(param)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new BaseResponsePredicate())
                    .subscribe(res -> {

                        res.isHeader = isheader;
                        datePage.setValue(res);

                    },throwable -> {

                        datePageError.setValue(isheader);
                        onError(throwable);

                    });
        }else  if(type==2){
            return BuildFactory.getService(StoreApi.class).getcheckindatelist_ot(json(param)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new BaseResponsePredicate())
                    .subscribe(res -> {

                        res.isHeader = isheader;
                        datePage.setValue(res);

                    },throwable -> {

                        datePageError.setValue(isheader);
                        onError(throwable);

                    });
        }
        else {
            return BuildFactory.getService(StoreApi.class).getDate(json(param)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new BaseResponsePredicate())
                    .subscribe(res -> {

                        res.isHeader = isheader;
                        datePage.setValue(res);

                    },throwable -> {

                        datePageError.setValue(isheader);
                        onError(throwable);

                    });
        }


    }

    public  Disposable getCheckMember (RecordPageParam param, boolean isheader,int type){

        if(type==1){
            return BuildFactory.getService(StoreApi.class).getCheckMemberOut(json(param)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new BaseResponsePredicate())
                    .subscribe(res -> {

                        res.isHeader = isheader;
                        checkMember.setValue(res);

                    },throwable -> {

                        checkMemberError.setValue(isheader);
                        onError(throwable);

                    });
        }else if(type==2){
            return BuildFactory.getService(StoreApi.class).getcheckinuserlist_ot(json(param)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new BaseResponsePredicate())
                    .subscribe(res -> {

                        res.isHeader = isheader;
                        checkMember.setValue(res);

                    },throwable -> {

                        checkMemberError.setValue(isheader);
                        onError(throwable);

                    });
        }else {
            return BuildFactory.getService(StoreApi.class).getCheckMember(json(param)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new BaseResponsePredicate())
                    .subscribe(res -> {

                        res.isHeader = isheader;
                        checkMember.setValue(res);

                    },throwable -> {

                        checkMemberError.setValue(isheader);
                        onError(throwable);

                    });
        }


    }

    public  Disposable setClock (ClockSetParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).clockSet(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {
                    clockSet.setValue(res.getResult().get(0));
                    progress.setValue(false);

                    onError(new Throwable(res.getMessage()));

                },throwable -> {

                    onError(throwable);

                });
    }
    public  Disposable setClockRange (ClockSetParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).clockSetRange(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {
                    clockSetRange.setValue(res.getResult().get(0));
                    progress.setValue(false);

                    onError(new Throwable(res.getMessage()));

                },throwable -> {

                    onError(throwable);

                });
    }

    public  Disposable getTodayCheck (UserParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).getTodayCheck(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {

                    todayCheck.setValue(res);
                    progress.setValue(false);


                },throwable -> {
                    todayCheck.setValue(null);
                    onError(throwable);

                });
    }

    public  Disposable sign (SignParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).sign(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {

                    sign.setValue(true);
                    progress.setValue(false);
                    onError(new Throwable(res.getMessage()));


                },throwable -> {
                    todayCheck.setValue(null);
                    onError(throwable);

                });
    }
    public  Disposable checkin2 (SignParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).sign2(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {

                    sign2.setValue(true);
                    progress.setValue(false);
                    onError(new Throwable(res.getMessage()));


                },throwable -> {
                    todayCheck.setValue(null);
                    onError(throwable);

                });
    }


    public  Disposable getTag (RankParam param){



        return BuildFactory.getService(StoreApi.class).getTags(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {


        tags.setValue(res.getResult());


                },throwable -> {

                    onError(throwable);

                });
    }

    public  Disposable getCategory (UserParam param){



        return BuildFactory.getService(StoreApi.class).getCategory(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {


                    categorys.setValue(res.getResult());


                },throwable -> {

                    categorys.setValue(null);
                    onError(throwable);

                });
    }

    public  Disposable getSubCategory (CategoryParam param){



        return BuildFactory.getService(StoreApi.class).getSubCategory(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {


                    subCategorys.setValue(res.getResult());


                },throwable -> {


                    onError(throwable);

                });
    }


    public  Disposable updateCategory (UpdateCategoryParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).updateCategory(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {


                    updateCategory.setValue(true);
                    progress.setValue(false);


                    onError(new Throwable(res.getMessage()));


                },throwable -> {

                    onError(throwable);

                });
    }
    public  Disposable deleteCategory (UpdateCategoryParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).deleteCategory(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {


                    deleteCategory.setValue(true);
                    progress.setValue(false);


                    onError(new Throwable(res.getMessage()));


                },throwable -> {

                    onError(throwable);

                });
    }
    public  Disposable addCategory (UpdateCategoryParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).addCategory(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {


                    addCategory.setValue(true);
                    progress.setValue(false);


                    onError(new Throwable(res.getMessage()));


                },throwable -> {

                    onError(throwable);

                });
    }

    public  Disposable addattendanceSign (StoreParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).addattendanceSign(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {


                    attanceSign.setValue(true);
                    progress.setValue(false);





                },throwable -> {

                    onError(throwable);

                });
    }
    public  Disposable outgoingsign (StoreParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).outgoingsign(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {


                    outgoingsign.setValue(true);
                    progress.setValue(false);





                },throwable -> {

                    onError(throwable);

                });
    }
    public  Disposable otsign (StoreParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).otsign(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {


                    otsign.setValue(true);
                    progress.setValue(false);





                },throwable -> {

                    onError(throwable);

                });
    }


    public  Disposable sortCategory (SortParam param){

        progress.setValue(true);

        return BuildFactory.getService(StoreApi.class).sortCategory(json(param) ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {


                    sortCategory.setValue(res.isSuccess());
                    progress.setValue(false);





                },throwable -> {
                    sortCategory.setValue(false);
                    onError(throwable);

                });
    }
    public final MutableLiveData<Boolean> workError = new MutableLiveData<>();
    public final MutableLiveData<OpusResponse> work = new MutableLiveData<>();

    //作品列表
    public  Disposable getCategoryWork (CategoryPagePram param, boolean isHeader){


        return BuildFactory.getService(LoginApi.class).getmyworkslistbycategoryid(json(param)).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {
                    res.isHeader = isHeader;
                    work.setValue(res);

                },throwable -> {

                    workError.setValue(isHeader);

                    onError(throwable);
                });
    }

    public  Disposable addworkspraise (CommentParam param, int position){


        return BuildFactory.getService(LoginApi.class).addworkspraise(json(param)).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new BaseResponsePredicate())
                .subscribe(res -> {

                    praise.setValue(res.getResult());
                    praisePosition.setValue(position);
                },throwable -> {

                    onError(throwable);

                });
    }
    public final MutableLiveData<Integer> praisePosition = new MutableLiveData<>();
    public final MutableLiveData<String> praise = new MutableLiveData<>();

}
